package com.ssi.myshop.ui.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.ssi.myshop.bean.BannerBean;

import java.util.ArrayList;
import java.util.List;

public class LooperPagerAdapter extends PagerAdapter {

    private List<BannerBean> mData = new ArrayList<>();

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        int width = container.getMeasuredWidth();
        int height = container.getMeasuredHeight();
        int ivSize = (width > height ? width : height) / 2;
        if (mData.size() > 0){
            int realPosition = position % mData.size();
            BannerBean bannerBean = mData.get(realPosition);
            ImageView iv = new ImageView(container.getContext());
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            iv.setLayoutParams(layoutParams);
            Glide.with(container.getContext()).load(bannerBean.getPicUrl()).into(iv);


            container.addView(iv);
            return iv;
        }
        return null;
    }

    public int getDataSize(){
        return mData.size();
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);

    }

    public void setData(List<BannerBean> data){
        mData.clear();
        mData.addAll(data);
        notifyDataSetChanged();
    }
}
